﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using OrBitDataModule.Common;
using OrBitDataModule.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OrBitDataModule.Contract
{
    public static class ScriptApi
    {

        public static async Task<(DataProtocolScript Script, string Message)> GetAllScript()
        {
            string operateApiName = "DataProtocolScript";
            var tokenResult = await WebApi.GetAccessTokenAsync(operateApiName, SharedData.LoginUserName, SharedData.LoginUserPassword);
            if (tokenResult.Result)
            {
                string token = tokenResult.Token;
                var (_, Response) = await WebApi.HttpPostAsync(operateApiName, token, new { OperateType = "GetAll" });

                DataProtocolScript scriptModel = JsonConvert.DeserializeObject<DataProtocolScript>(Response);
                return (scriptModel, string.Empty);
            }
            return (null, tokenResult.Message);
        }

        public static async Task<DataTable> GetList()
        {
            string operateApiName = "DataProtocolScript";
            var tokenResult = await WebApi.GetAccessTokenAsync(operateApiName, SharedData.LoginUserName, SharedData.LoginUserPassword);
            if (tokenResult.Result)
            {
                string token = tokenResult.Token;
                var (_, Response) = await WebApi.HttpPostAsync(operateApiName, token, new { OperateType = "GetList" });

                JObject jsonObject = JObject.Parse(Response);
                JArray dataArray = (JArray)jsonObject["Data"];
                DataTable dataTable = new DataTable();
                dataTable.Columns.Add("DataProtocolScriptId");
                dataTable.Columns.Add("ScriptName");
                foreach (var data in dataArray)
                {
                    string dataProtocolId = data["DataProtocolScriptId"].ToString();
                    string protocolName = data["ScriptName"].ToString();

                    DataRow row = dataTable.NewRow();
                    row["DataProtocolScriptId"] = dataProtocolId;
                    row["ScriptName"] = protocolName;
                    dataTable.Rows.Add(row);
                }
                return dataTable;
            }
            return null;
        }

        public static (string ScriptName, string ScriptCode, string ScriptDescription, string CreateUser) GetCode(string dataProtocolScriptId)
        {
            string operateApiName = "DataProtocolScript";
            var tokenResult =  WebApi.GetAccessToken(operateApiName, SharedData.LoginUserName, SharedData.LoginUserPassword);
            if (tokenResult.Result)
            {
                string token = tokenResult.Token;
                var (_, Response) = WebApi.HttpPost(operateApiName, token, new { OperateType = "Get", DataProtocolScriptId = dataProtocolScriptId });

                JObject jsonObject = JObject.Parse(Response);
                string scriptName = jsonObject["ScriptName"].Value<string>();
                string scriptCode = jsonObject["ScriptCode"].Value<string>();
                string scriptDescription = jsonObject["ScriptDescription"].Value<string>();
                string createUser = jsonObject["CreateUser"].Value<string>();
                return (scriptName, scriptCode, scriptDescription, createUser);
            }
            return (string.Empty, string.Empty, string.Empty, string.Empty);
        }

        public static async Task<(string ScriptName, string ScriptCode, string ScriptDescription, string CreateUser)> GetCodeAsync(string dataProtocolScriptId)
        {
            string operateApiName = "DataProtocolScript";
            var tokenResult = await WebApi.GetAccessTokenAsync(operateApiName, SharedData.LoginUserName, SharedData.LoginUserPassword);
            if (tokenResult.Result)
            {
                string token = tokenResult.Token;
                var (_, Response) = await WebApi.HttpPostAsync(operateApiName, token, new { OperateType = "Get", DataProtocolScriptId = dataProtocolScriptId });

                JObject jsonObject = JObject.Parse(Response);
                string scriptName = jsonObject["ScriptName"].Value<string>();
                string scriptCode = jsonObject["ScriptCode"].Value<string>();
                string scriptDescription = jsonObject["ScriptDescription"].Value<string>();
                string createUser = jsonObject["CreateUser"].Value<string>();
                return (scriptName, scriptCode, scriptDescription, createUser);
            }
            return (string.Empty, string.Empty, string.Empty, string.Empty);
        }

        public static async Task<(bool Result, string Message)> RemoveScript(string dataProtocolScriptId)
        {
            string operateApiName = "DataProtocolScript";
            var tokenResult = await WebApi.GetAccessTokenAsync(operateApiName, SharedData.LoginUserName, SharedData.LoginUserPassword);
            if (tokenResult.Result)
            {
                string token = tokenResult.Token;
                var (_, Response) = await WebApi.HttpPostAsync(operateApiName, token, new
                {
                    OperateType = "Delete",
                    DataProtocolScriptId = dataProtocolScriptId
                });
                JObject obj = JObject.Parse(Response);
                bool result = obj["IsSuccess"].Value<bool>();
                string message = obj["ReturnMessage"].Value<string>();
                return (result, message);
            }
            return (false, tokenResult.Message);
        }

        public static async Task<(bool Result, string Message)> AddScript(string dataProtocolScriptId, string scriptName, string scriptCode, string scriptDescription)
        {
            string operateApiName = "DataProtocolScript";
            var tokenResult = await WebApi.GetAccessTokenAsync(operateApiName, SharedData.LoginUserName, SharedData.LoginUserPassword);
            if (tokenResult.Result)
            {
                string token = tokenResult.Token;
                var (_, Response) = await WebApi.HttpPostAsync(operateApiName, token, new
                {
                    OperateType = "Add",
                    DataProtocolScriptId = dataProtocolScriptId,
                    ScriptName = scriptName,
                    ScriptCode = Convert.ToBase64String(Encoding.Default.GetBytes(scriptCode)),
                    ScriptDescription = scriptDescription,
                    OrBitUserName = SharedData.LoginUserName,
                });
                JObject obj = JObject.Parse(Response);
                bool result = obj["IsSuccess"].Value<bool>();
                string message = obj["ReturnMessage"].Value<string>();
                return (result, message);
            }
            return (false, tokenResult.Message);
        }
    }
}
